package mock.ab_sd.web.action;

import java.util.ArrayList;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;

import mock.ab_sd.business.GovernmentBOImpl;
import mock.ab_sd.common.utility.FilterListType;
import mock.ab_sd.common.utility.ListPaging;
import mock.ab_sd.common.utility.OrderCriteria;
import mock.ab_sd.common.valueobject.GovernmentVO;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * GovernmentOfficeRegionAction.java GovernmentOfficeRegionAction Class * 
 * @author HAI
 * 
 */
public class GovernmentOfficeRegionAction extends BaseAction{
	private static final long serialVersionUID = 1L;

	// declare an array list to store list of government office region
	private ArrayList<GovernmentVO> governmentList;
	// declare ListPaging object
	private ListPaging listPaging;
	// declare a boolean variable to store value of check box in
	// ListGovernmentOfficeRegion.jsp
	private boolean inActive = false;	

	
	/**
	 * Author: HAI
	 * Created date : 17/08/2012
	 * Get requests from ListGovernmentOfficeRegion.jsp and return list of 
	 * government office region
	 */
	@SuppressWarnings("unchecked")
	public String execute() {
		// declare an GovernmnetBO object
		GovernmentBOImpl governmentBO = new GovernmentBOImpl();

		if (listPaging == null) {
			// initializing ListPaging
			setListPaging(new ListPaging());
		}
		
		if (listPaging.getOrderBy() == null) {
            listPaging.setOrderBy("GovOfficeRegionName");
        }
		
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		String filter = request.getParameter("filter");
		@SuppressWarnings("rawtypes")
		Map session = ActionContext.getContext().getSession();
		try{
			if(!filter.equals(null)){
				session.put("filter", filter);
			}
			else{
				filter = (String) session.get("filter");
			}
		}catch(NullPointerException e){
			filter = (String) session.get("filter");
		}
		// set record per page = 15
		getListPaging().setRecordPerPage(15);
		// set filter
		try{
			getListPaging().setFilter(FilterListType.valueOf(filter));
		} catch (Exception e){
			//getListPaging().setFilter(FilterListType.ALL);
			e.printStackTrace();
		}

		// set order criteria
		getListPaging().setOrderCriteria(OrderCriteria.ASC);

		// get list of government office region by status of check box
		if (!inActive) { // check box "Include In-Active" is not checked
			listPaging.setInActive(false);
			try {
				// get government office region list
				setGovernmentList((governmentBO.listGovernment(getListPaging())));
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else { // check box "Include In-Active" is checked
			listPaging.setInActive(true);
			try {
				// get government office region list
				setGovernmentList((governmentBO.listGovernment(getListPaging())));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		/* return SUCCESS to display ListGovernmentOfficeRegion.jsp */
		return SUCCESS;
	}

	// set governmentList
	public void setGovernmentList(ArrayList<GovernmentVO> governmentList) {
		this.governmentList = governmentList;
	}

	// get governmentList
	public ArrayList<GovernmentVO> getGovernmentList() {
		return governmentList;
	}

	// get listPaging
	public ListPaging getListPaging() {
		return listPaging;
	}

	// set listPaging
	public void setListPaging(ListPaging listPaging) {
		this.listPaging = listPaging;
	}

	// set in-active
	public void setInActive(boolean inActive) {
		this.inActive = inActive;
	}

	// get in-active
	public boolean isInActive() {
		return inActive;
	}
}
